//  组合模式典型应用之文件夹和文件
// 文件夹类
function Folder(name) { 
    this.name = name;
    this.children = [];
    this.parent = null;
 }

Folder.prototype.add = function (child) { 
    child.parent = this;
    this.children.push(child);
}

Folder.prototype.show = function () { 
    // 打印当前文件夹名称
    console.log('文件夹：', this.name);
    // 遍历文件夹下的所有文件
    for (var i = 0; i < this.children.length; i++) {
        // 打印出文件下每一个文件的名称
        this.children[i].show();
    }
 }


Folder.prototype.remove = function () { 
     // 删除当前文件夹下所有文件
     if (!this.parent) return;
     let children = this.parent.children;
     for (var i =0; i < children.length; i++) {
         children[i] == this && children.splice(i, 1);
     }
}


// 文件类
function File(name) { 
    this.name = name;
}

File.prototype.add= function () { 
    throw new Error('文件下不可以再创建文件！')
}

File.prototype.show= function () { 
    console.log('文件名称：', this.name);
}


// 测试
let video = new Folder('video');
let mp4 = new Folder('mp4');
let mp5 = new Folder('mp5');
video.add(mp4);
video.add(mp5);

let file1 = new File('test01');
let file2 = new File('test02');
let file3 = new File('test03');

// video.add(file1);
mp4.add(file2);
mp5.add(file3);
// file3.add(file1);

video.show();
console.log('================');

mp5.remove();
video.show();

